using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Blople.GameComponents
{
    public class InputField : Microsoft.Xna.Framework.DrawableGameComponent
    {
        protected Texture2D texture;
        protected Texture2D cursorTexture;

        protected SpriteFont font;

        protected Vector2 position;

        protected String inputText;
        protected int MIN = 3;
        protected int MAX = 20;

        String letter = "";

        KeyboardState keyboardState;
        KeyboardState oldKeyboardState;

        public InputField(Game game, Texture2D texture, Texture2D cursorTexture, SpriteFont font) : base(game)
        {
            this.texture = texture;
            this.cursorTexture = cursorTexture;
            this.font = font;
            position = new Vector2(50, 400);
            inputText = "";
        }

        public override void Initialize()
        {
            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            HandleInput();
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            sBatch.Draw(texture, position, Color.White);
            sBatch.DrawString(font, inputText, new Vector2(position.X + 10, position.Y + 5), Color.Black);
            sBatch.Draw(cursorTexture, new Vector2(position.X + 11 +font.MeasureString(inputText).X, position.Y + 7), Color.White);
            base.Draw(gameTime);
        }

        public void AddLetter(String letter)
        {
            if (inputText.Length < MAX) 
            {
                inputText += letter;
            }
        }

        public void DeleteLastLetter()
        {
            if (inputText.Length > 0) 
            {
                inputText = inputText.Substring(0, inputText.Length - 1);
            }
        }

        public Boolean Validate()
        {
            if (inputText.Length >= MIN)
            {
                return true;
            } 
            else 
            {
                return false;
            }
        }

        public String InputText
        {
            get { return inputText; }
            set { inputText = value; }
        }

        public void HandleInput()
        {
            letter = "";

            keyboardState = Keyboard.GetState();

            Boolean keyA = (oldKeyboardState.IsKeyDown(Keys.A) && keyboardState.IsKeyUp(Keys.A));
            if (keyA) 
            {
                letter = Keys.A.ToString();
            }

            Boolean keyB = (oldKeyboardState.IsKeyDown(Keys.B) && keyboardState.IsKeyUp(Keys.B));
            if (keyB)
            {
                letter = Keys.B.ToString();
            }

            Boolean keyC = (oldKeyboardState.IsKeyDown(Keys.C) && keyboardState.IsKeyUp(Keys.C));
            if (keyC)
            {
                letter = Keys.C.ToString();
            }

            Boolean keyD = (oldKeyboardState.IsKeyDown(Keys.D) && keyboardState.IsKeyUp(Keys.D));
            if (keyD)
            {
                letter = Keys.D.ToString();
            }

            Boolean keyE = (oldKeyboardState.IsKeyDown(Keys.E) && keyboardState.IsKeyUp(Keys.E));
            if (keyE)
            {
                letter = Keys.E.ToString();
            }

            Boolean keyF = (oldKeyboardState.IsKeyDown(Keys.F) && keyboardState.IsKeyUp(Keys.F));
            if (keyF)
            {
                letter = Keys.F.ToString();
            }

            Boolean keyG = (oldKeyboardState.IsKeyDown(Keys.G) && keyboardState.IsKeyUp(Keys.G));
            if (keyG)
            {
                letter = Keys.G.ToString();
            }

            Boolean keyH = (oldKeyboardState.IsKeyDown(Keys.H) && keyboardState.IsKeyUp(Keys.H));
            if (keyH)
            {
                letter = Keys.H.ToString();
            }

            Boolean keyI = (oldKeyboardState.IsKeyDown(Keys.I) && keyboardState.IsKeyUp(Keys.I));
            if (keyI)
            {
                letter = Keys.I.ToString();
            }

            Boolean keyJ = (oldKeyboardState.IsKeyDown(Keys.J) && keyboardState.IsKeyUp(Keys.J));
            if (keyJ)
            {
                letter = Keys.J.ToString();
            }

            Boolean keyK = (oldKeyboardState.IsKeyDown(Keys.K) && keyboardState.IsKeyUp(Keys.K));
            if (keyK)
            {
                letter = Keys.K.ToString();
            }

            Boolean keyL = (oldKeyboardState.IsKeyDown(Keys.L) && keyboardState.IsKeyUp(Keys.L));
            if (keyL)
            {
                letter = Keys.L.ToString();
            }

            Boolean keyM = (oldKeyboardState.IsKeyDown(Keys.M) && keyboardState.IsKeyUp(Keys.M));
            if (keyM)
            {
                letter = Keys.M.ToString();
            }

            Boolean keyN = (oldKeyboardState.IsKeyDown(Keys.N) && keyboardState.IsKeyUp(Keys.N));
            if (keyN)
            {
                letter = Keys.N.ToString();
            }

            Boolean keyO = (oldKeyboardState.IsKeyDown(Keys.O) && keyboardState.IsKeyUp(Keys.O));
            if (keyO)
            {
                letter = Keys.O.ToString();
            }

            Boolean keyP = (oldKeyboardState.IsKeyDown(Keys.P) && keyboardState.IsKeyUp(Keys.P));
            if (keyP)
            {
                letter = Keys.P.ToString();
            }

            Boolean keyQ = (oldKeyboardState.IsKeyDown(Keys.Q) && keyboardState.IsKeyUp(Keys.Q));
            if (keyQ)
            {
                letter = Keys.Q.ToString();
            }

            Boolean keyR = (oldKeyboardState.IsKeyDown(Keys.R) && keyboardState.IsKeyUp(Keys.R));
            if (keyR)
            {
                letter = Keys.R.ToString();
            }

            Boolean keyS = (oldKeyboardState.IsKeyDown(Keys.S) && keyboardState.IsKeyUp(Keys.S));
            if (keyS)
            {
                letter = Keys.S.ToString();
            }

            Boolean keyT = (oldKeyboardState.IsKeyDown(Keys.T) && keyboardState.IsKeyUp(Keys.T));
            if (keyT)
            {
                letter = Keys.T.ToString();
            }

            Boolean keyU = (oldKeyboardState.IsKeyDown(Keys.U) && keyboardState.IsKeyUp(Keys.U));
            if (keyU)
            {
                letter = Keys.U.ToString();
            }

            Boolean keyV = (oldKeyboardState.IsKeyDown(Keys.V) && keyboardState.IsKeyUp(Keys.V));
            if (keyV)
            {
                letter = Keys.V.ToString();
            }

            Boolean keyW = (oldKeyboardState.IsKeyDown(Keys.W) && keyboardState.IsKeyUp(Keys.W));
            if (keyW)
            {
                letter = Keys.W.ToString();
            }

            Boolean keyX = (oldKeyboardState.IsKeyDown(Keys.X) && keyboardState.IsKeyUp(Keys.X));
            if (keyX)
            {
                letter = Keys.X.ToString();
            }

            Boolean keyY = (oldKeyboardState.IsKeyDown(Keys.Y) && keyboardState.IsKeyUp(Keys.Y));
            if (keyY)
            {
                letter = Keys.Y.ToString();
            }

            Boolean keyZ = (oldKeyboardState.IsKeyDown(Keys.Z) && keyboardState.IsKeyUp(Keys.Z));
            if (keyZ)
            {
                letter = Keys.B.ToString();
            }

            if (inputText == "") 
            {
                AddLetter(letter.ToUpper());
            } 
            else 
            {
                AddLetter(letter.ToLower());
            }

            Boolean keyBack = (oldKeyboardState.IsKeyDown(Keys.Back) && keyboardState.IsKeyUp(Keys.Back));

            if (keyBack)
            {
                DeleteLastLetter();
            }

            oldKeyboardState = keyboardState;
        }
    }
}
